package com.weixin.project.Config;

import com.weixin.project.Config.Filter.CurrentUserFilter;
import com.weixin.project.Config.Filter.RateLimitFilter;
import com.weixin.project.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;

import java.util.concurrent.TimeUnit;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserService userService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().disable();
        int maxRequests = 10;
        long timeWindowMillis = TimeUnit.SECONDS.toMillis(1);
        RateLimitFilter rateLimitFilter = new RateLimitFilter(maxRequests, timeWindowMillis);
        CurrentUserFilter currentUserFilter = new CurrentUserFilter(userService);
        http.csrf().disable() // 禁用CSRF保护
                .addFilterBefore(rateLimitFilter, BasicAuthenticationFilter.class)
                .addFilterBefore(currentUserFilter, RateLimitFilter.class);
    }
}
